package com.zhku.laosiji.controller;

import com.zhku.laosiji.pojo.Admin;
import com.zhku.laosiji.service.AdminService;
import com.zhku.laosiji.utils.ResultUtils;
import com.zhku.laosiji.utils.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 登录
     * @param name
     * @param password
     * @param model
     * @param session
     * @return
     */
    @PostMapping("/login")
    public String login(@RequestParam("name") String name,
                        @RequestParam("password") String password,
                        Model model,
                        HttpSession session) {
        Admin admin = adminService.selectByName(name);
        if (admin.getPassword().equals(password)) {
            session.setAttribute("admin",admin);
            return "index";
        } else {
            model.addAttribute("loginError", true);
            model.addAttribute("errormsg","账号或密码错误！");
            return "login";
        }

    }

    /**
     * 显示欢迎页面
     * @return
     */
    @RequestMapping("/welcome")
    public String welcome() {
        return "welcome";
    }

    /**
     * 显示欢迎页面
     * @return
     */
    @RequestMapping("/changepassword")
    public String changepassword() {
        return "change-password";
    }

    /**
     * 注销
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("admin");
        return "login";
    }

    /**
     * 保存管理员修改的新密码
     * @param newpassword
     * @param session
     * @return
     */
    @RequestMapping("/changePassword")
    @ResponseBody
    public ResultVO changepassword( @RequestParam("newpassword") String newpassword,
                                    HttpSession session){
        //获取当前登录管理员
        Admin admin = (Admin)session.getAttribute("admin");
        admin.setPassword(newpassword);
        try {
            adminService.updateAdmin(admin);
        } catch (Exception e) {
            return ResultUtils.error(1, "修改失败");
        }
        session.setAttribute("admin", admin);
        return ResultUtils.success();
    }

    //根据名字查询管理员
    @RequestMapping("/getadminbyname")
    public ModelAndView getAdminByName(@RequestParam("name")String name,Map<String,Object> map){
        List<Admin> admin = adminService.selectAdminByNameLike(name);
        if(admin != null){
            map.put("admins",admin);
        }
        return new ModelAndView("admin-list",map);
    }

    //管理员添加页面
    @RequestMapping("/addadminpage")
    public ModelAndView addAdminPage(){
        return new ModelAndView( "/admin-add");
    }

    //管理员添加
    @RequestMapping("/addadmin")
    public String addAdmin(Admin admin){
        Admin ad = adminService.selectByName(admin.getName());
        if(admin!=null&&ad==null){
            admin.setCreatedate(new Date());
            adminService.addAdmin(admin);
        }
        return "redirect:/admin/admins";
    }


    @GetMapping("/admins")
    public ModelAndView getAdminList(){
        Collection<Admin> admins = adminService.getAdminList();
        ModelAndView modelAndView = new ModelAndView("admin-list");
        modelAndView.addObject("admins",admins);
        return modelAndView;
    }

    @RequestMapping("/deleteAdmin")
    @ResponseBody
    public ResultVO deleteAdmin(int id){
        int orsuccess = adminService.deleteAdmin(id);
        if (orsuccess == 1) {

            return ResultUtils.success();
        }else {
            return ResultUtils.error(0,"删除失败！");
        }
    }

    @RequestMapping("/")
    public String index() {
        return "login";
    }
}
